package com.ruoyi.personnel.oa.leave.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.oa.leave.domain.LeaveCond;
import com.ruoyi.personnel.oa.leave.domain.LeaveDomain;
import com.ruoyi.personnel.oa.leave.domain.LeaveDto;
import com.ruoyi.personnel.oa.leave.service.ILeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 请假
 * 控制层接口
 */
@RestController
@RequestMapping("/personal/oa/leave")
@Api(value = "请假模块", tags = "请假接口")
public class LeaveController extends BaseController {
    @Autowired
    private ILeaveService leaveService;

    /**
     * 获取请假
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取请假列表",notes = "查询所有请假信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody LeaveCond cond) {
        startPage();
        List<LeaveDto> leaveDtos = leaveService.selectLeaveList(cond);
        return getDataTable(leaveDtos);
    }

    /**
     * 导出请假
     *
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody LeaveCond cond) {
        List<LeaveDto> leaveDtos = leaveService.selectLeaveList(cond);
        ExcelUtil<LeaveDto> util = new ExcelUtil<>(LeaveDto.class);
        return util.exportExcel(leaveDtos, "请假单数据");
    }

    /**
     * 新增请假
     * @param leave
     * @return
     */
    @ApiOperation(value = "新增请假", notes = "新增请假")
    @ApiImplicitParam(name = "leave", value = "新增请假", dataType = "LeaveDomain")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LeaveDomain leave) {
        try {
            leaveService.insertLeave(leave);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 更新请假
     * @param leave
     * @return
     */
    @ApiOperation(value = "修改请假", notes = "修改请假")
    @ApiImplicitParam(name = "leave", value = "修改请假", dataType = "LeaveDomain")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody LeaveDomain leave) {
        try {
            leaveService.updateLeave(leave);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除请假
     * 仅修改删除标志位
     * @param leave
     * @return
     */
    @ApiOperation(value = "删除请假", notes = "删除请假,仅删除标志位")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody LeaveDomain leave) {

        return toAjax(leaveService.updateLeaveDeleteFlag(leave));
    }
}
